<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Albums extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Images_model', '', TRUE);
        $this->load->model('Albums_model', '', TRUE);
    }

    //ajax 提交
    public function upload_image() {
        $data['image'] = $this->Images_model->upload_image();
        $this->load->view('albums/upload_image', $data);
    }

    //默认列表
    public function index() {
        $this->load->library('pagination');

        $config['base_url'] = '/albums/index';
        $config['total_rows'] = $this->db->count_all('albums');
        $config['per_page'] = 12;
        $config['uri_segment'] = 3;

        $this->pagination->initialize($config);
        $data['albums'] = $this->Albums_model->get_albums($config['per_page'], $this->uri->segment(3));
        $data['page_title'] = '相册';
        $data['page_description'] = '我的相册';
        $data['page_keywords'] = 'album';

        $this->load->view('albums/index', $data);
    }

    /**
     * @param $album_id 相册id
     */
    public function view($album_id) {
        $this->load->library('pagination');

        $config['base_url'] = '/albums/view/' . $album_id . '/page/';
        $config['total_rows'] = $this->Images_model->get_count_by_album_id($album_id);
        $config['per_page'] = 12;
        $config['uri_segment'] = 5;
        $this->pagination->initialize($config);

        $data['page_title'] = '相册';
        $data['page_description'] = '我的相册';
        $data['page_keywords'] = 'album';
        $data['images'] = $this->Images_model->get_image_by_album_id($album_id, $config['per_page'], $this->uri->segment(5));
        $this->load->view('albums/view', $data);
    }

    /**
     * @param $id image id
     */
    public function image($id) {
        $this->load->library('pagination');
        $this->load->model('Comments_model', '', TRUE);
        $this->load->helper('gravatar');

        $data['image'] = $this->Images_model->get_image_by_id($id);
        if ($data['image']) {
            $data['image_pre'] = $this->Images_model->get_pre_image($id, $data['image']->album_id);
            $data['image_next'] = $this->Images_model->get_next_image($id, $data['image']->album_id);
        }

        $data['dtype'] = 2;
        $data['did'] = $data['image']->ID;
        $data['page_title'] = $data['image']->image_description . '相册';
        $data['page_description'] = $data['image']->image_description;
        $data['page_keywords'] = 'album,相册';
        
        $config['base_url'] = '/articles/' .  $data['image']->ID . '/page/';
        $config['total_rows'] = $data['image']->comment_count;
        $config['per_page'] = 12;
        $config['uri_segment'] = 5;
        $this->pagination->initialize($config);
        $data['comments'] = $this->Comments_model->get_comments($data['image']->ID, 2, $config['per_page'], $this->uri->segment(5));

        $this->load->view('albums/image', $data);
    }

    public function upload() {
        $this->load->model('Users_model', '', TRUE);
        $this->Users_model->is_logged_in();

        $data['page_title'] = '相册上传图片';
        $data['page_description'] = '相册上传图片';
        $data['page_keywords'] = '相册上传图片';
        $this->load->view('albums/upload', $data);
    }

}